package com.yl.applicationsecurity.utils;
import lombok.var;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ExcelExportUtil {


    /**
     * 导出数据为Excel
     * @param dataList 要导出的数据列表
     * @param headers 表头内容
     * @param columns 表头与数据的映射
     * @param <T> 数据类型
     * @return Excel文件的字节流
     * @throws IOException
     */
    public static <T> ByteArrayOutputStream exportToApplicationCoverageExcel(List<T> dataList, List<String> headers, List<String> columns) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Data");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.size(); i++) {
            headerRow.createCell(i).setCellValue(headers.get(i));
        }

        // 填充数据
        int rowNum = 1;
        for (T data : dataList) {
            Row row = sheet.createRow(rowNum++);
            for (int i = 0; i < columns.size(); i++) {
                String column = columns.get(i);
                Object value = getPropertyValue(data, column); // 获取对象的属性值

                if (column.equals("isInternet")){//判断是否为互联网数据
                    if(value.toString().equals("0")){
                        value = "互联网不可访问";
                    }else  if(value.toString().equals("1")){
                        value = "互联网可访问";
                    }

                }

                if (column.equals("iastStatus")){//判断是否为互联网数据
                    if(value.toString().equals("0")){
                        value = "IAST未覆盖";
                    }else  if(value.toString().equals("1")){
                        value = "IAST已覆盖";
                    }else  if(value.toString().equals("2")){
                        value = "IAST无需覆盖";
                    }

                }

                if (column.equals("raspStatus")){//判断是否为互联网数据
                    if(value.toString().equals("0")){
                        value = "RASP未覆盖";
                    }else  if(value.toString().equals("1")){
                        value = "RASP已覆盖";
                    }else  if(value.toString().equals("2")){
                        value = "RASP无需覆盖";
                    }

                }


                if (column.equals("logicProbeStatus")){//判断是否为互联网数据
                    if(value.toString().equals("0")){
                        value = "逻辑探针未覆盖";
                    }else  if(value.toString().equals("1")){
                        value = "逻辑探针已覆盖";
                    }else  if(value.toString().equals("2")){
                        value = "逻辑探针无需覆盖";
                    }

                }



                row.createCell(i).setCellValue(value != null ? value.toString() : "");
            }
        }

        // 自动调整列宽
        for (int i = 0; i < columns.size(); i++) {
            sheet.autoSizeColumn(i);
        }

        // 将Excel数据写入ByteArrayOutputStream
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        return outputStream;
    }




    /**
     * 导出数据为Excel
     * @param dataList 要导出的数据列表
     * @param headers 表头内容
     * @param columns 表头与数据的映射
     * @param <T> 数据类型
     * @return Excel文件的字节流
     * @throws IOException
     */
    public static <T> ByteArrayOutputStream exportToExcel(List<T> dataList, List<String> headers, List<String> columns) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Data");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.size(); i++) {
            headerRow.createCell(i).setCellValue(headers.get(i));
        }

        // 填充数据
        int rowNum = 1;
        for (T data : dataList) {
            Row row = sheet.createRow(rowNum++);
            for (int i = 0; i < columns.size(); i++) {
                String column = columns.get(i);
                Object value = getPropertyValue(data, column); // 获取对象的属性值
                row.createCell(i).setCellValue(value != null ? value.toString() : "");
            }
        }

        // 自动调整列宽
        for (int i = 0; i < columns.size(); i++) {
            sheet.autoSizeColumn(i);
        }

        // 将Excel数据写入ByteArrayOutputStream
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        return outputStream;
    }

    /**
     * 使用反射获取对象的属性值
     * @param obj 对象
     * @param property 属性名
     * @return 属性值
     */
    private static Object getPropertyValue(Object obj, String property) {
        try {
            var field = obj.getClass().getDeclaredField(property);
            field.setAccessible(true);
            return field.get(obj);
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 导出数据为Excel
     * @param dataList 要导出的数据列表
     * @param headers 表头内容
     * @param columns 表头与数据的映射
     * @param <T> 数据类型
     * @return Excel文件的字节流
     * @throws IOException
     */
    public static <T> ByteArrayOutputStream exportToTodoLisrExcel(List<Map> dataList, List<String> headers, List<String> columns) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Data");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.size(); i++) {
            headerRow.createCell(i).setCellValue(headers.get(i));
        }

        // 填充数据
        int rowNum = 1;
        for (Map<String,Object> data : dataList) {
            Row row = sheet.createRow(rowNum++);
            for (int i = 0; i < columns.size(); i++) {
                String column = columns.get(i);
                Object value = data.get( column); // 获取对象的属性值
                row.createCell(i).setCellValue(value != null ? value.toString() : "");
            }
        }

        // 自动调整列宽
        for (int i = 0; i < columns.size(); i++) {
            sheet.autoSizeColumn(i);
        }

        // 将Excel数据写入ByteArrayOutputStream
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        return outputStream;
    }

}
